package com.fullertontech.framework.fileupload.fastdfs;

import java.io.File;
import java.io.IOException;
import javax.annotation.PostConstruct;
import org.apache.commons.io.FileUtils;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import com.fullertontech.framework.config.ConfigClient;
import com.fullertontech.framework.fileupload.FileObject;
import com.fullertontech.framework.fileupload.FileUploadClient;
import com.fullertontech.framework.fileupload.FileUploadClientGroup;

class FastDfsClient implements FileUploadClientGroup,FileUploadClient{
  
  @Override
  public FileUploadClient get(String bucketName) {
    return this;
  }
  
  private final ConfigClient configClient;
  private final String trackerNginxUrl;
  public FastDfsClient(ConfigClient configClient) {
    this.configClient=configClient;
    this.trackerNginxUrl=configClient.getProperty("fastdfs.tracker.nginx_url","");
  }
  
  @PostConstruct
  public void initFastdfs() {
    try {
      ClientGlobal.initByProperties(configClient.properties());
    } catch (Exception e) {
      e.printStackTrace();
    }
    System.out.println("init fastdfs");
  }

  private StorageClient createFastDfsClient() throws IOException {
    TrackerClient trackerClient = new TrackerClient();
    TrackerServer trackerServer = trackerClient.getConnection();
    StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
    StorageClient storageClient = new StorageClient(trackerServer, storageServer);
    return storageClient;
  }

  @Override
  public FileObject upload(File file, String ext, String fileName) throws Exception {
    NameValuePair[] meta_list = new NameValuePair[] {new NameValuePair("ext", ext),
        new NameValuePair("fileName", fileName)};
    StorageClient storageClient = createFastDfsClient();
    byte[] fileBytes=FileUtils.readFileToByteArray(file);
    String[] uploadResults = storageClient.upload_file(fileBytes, ext, meta_list);
    if (uploadResults != null && uploadResults.length == 2) {
      String groupName = uploadResults[0];
      String remoteFileName = uploadResults[1];
      String fileAbsolutePath =
          String.format("%s/%s/%s", trackerNginxUrl, groupName, remoteFileName);
      return new FastdfsFileObject() {
        
        @Override
        public String url() {
          return fileAbsolutePath;
        }
        
        @Override
        protected String remoteFilename() { 
          return remoteFileName;
        }
        
        @Override
        protected String groupName() {
          return groupName;
        }
      };
    }
    return null;
  }

  @Override
  public void delete(FileObject fileObject) throws Exception {
    if(fileObject instanceof FastdfsFileObject) {
      StorageClient storageClient = createFastDfsClient();
      storageClient.delete_file(fileObject.fileId()[0], fileObject.fileId()[1]);
    }else {
      throw new RuntimeException("fileObject type not FastdfsFileObject");
    }
  }

  @Override
  public String getFileUrl(FileObject fileObject) throws Exception {
    if(fileObject instanceof FastdfsFileObject) {
      return String.format("%s/%s/%s", trackerNginxUrl,fileObject.fileId()[0], fileObject.fileId()[1]);
    }else {
      throw new RuntimeException("fileObject type not FastdfsFileObject");
    }
  }

  @Override
  public String getFileUrl(FileObject fileObject, int width, int height) throws Exception {
    return getFileUrl(fileObject);
  }
}
